<?
$mysql_host = "mysql7.000webhost.com";
$mysql_database = "a4145141_cd";
$mysql_user = "a4145141_cd";
$mysql_password = "france8";

$mysqli = new mysqli(
		$mysql_host, 
		$mysql_user, 
		$mysql_password, 
		$mysql_database);

#header("Access-Control-Allow-Origin: *");
#header("Access-Control-Allow-Methods: PUT, GET, POST, DELETE, OPTIONS");
#header("Access-Control-Allow-Headers: *");

if (mysqli_connect_errno()) {
    //printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

function get_client_ip() {
     $ipaddress = '';
     if ($_SERVER['HTTP_CLIENT_IP'])
         $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
     else if($_SERVER['HTTP_X_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
     else if($_SERVER['HTTP_X_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
     else if($_SERVER['HTTP_FORWARDED_FOR'])
         $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
     else if($_SERVER['HTTP_FORWARDED'])
         $ipaddress = $_SERVER['HTTP_FORWARDED'];
     else if($_SERVER['REMOTE_ADDR'])
         $ipaddress = $_SERVER['REMOTE_ADDR'];
     else
         $ipaddress = 'UNKNOWN';

     return $ipaddress; 
}

$fp = "undefined";
$cs = "undefined";
$ip = get_client_ip();

if ($_REQUEST["fp"]) {
	$fp = $mysqli->real_escape_string($_REQUEST["fp"]);
}

if ($_REQUEST["cs"]) {
	$cs = $mysqli->real_escape_string($_REQUEST["cs"]);
}

$query = "INSERT INTO `usage` (`callsign`, `ipaddress`, `fingerprint`) VALUES ('$cs','$ip','$fp')";

if ($mysqli->query($query)) {
	printf("pong");
} else {
	printf("p0ng");
	//printf("failed to insert, data received:<br>");
	//printf("fp:%s<br>",$fp);
	//printf("cs:%s<br>",$cs);
	//printf("ip:%s<br>",$ip);
	//printf("query: %s<br>",$query);
	//printf("Error: %s<br>", $mysqli->error);
}

$mysqli->close();

?>

